package com.xoms.admin.controller.springmvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;
import com.xoms.admin.controller.res.*;
import com.xoms.admin.controller.res.bean.*;
import com.xoms.admin.controller.springmvc.req.*;

/**
 * 账号相关接口
 **/
@Controller
@RequestMapping(value = "/account", produces = "application/json")
public class SAccountController {
	@Autowired
	com.xoms.admin.controller.AccountController accountController;

	/**
	 * 验证用户token
	 **/
	@RequestMapping("/v1/checkToken")
	@ResponseBody
	public ObjectResult<RAccountLoginBean> checkToken(@Validated CheckTokenParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RAccountLoginBean> res = new ObjectResult<RAccountLoginBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return accountController.checkToken(param, request, response);
	}

	/**
	 * 登录
	 **/
	@RequestMapping("/v1/login")
	@ResponseBody
	public ObjectResult<RAccountLoginBean> login(@Validated LoginParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RAccountLoginBean> res = new ObjectResult<RAccountLoginBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return accountController.login(param, request, response);
	}

	/**
	 * 发送验证码
	 **/
	@RequestMapping("/v1/sendVCode")
	@ResponseBody
	public BaseResult sendVCode(@Validated SendVCodeParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return accountController.sendVCode(param, request, response);
	}

	/**
	 * 验证验证码
	 **/
	@RequestMapping("/v1/vertifyVCode")
	@ResponseBody
	public ObjectResult<RVertifyVCodeBean> vertifyVCode(@Validated VertifyVCodeParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RVertifyVCodeBean> res = new ObjectResult<RVertifyVCodeBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return accountController.vertifyVCode(param, request, response);
	}

	/**
	 * 找回密码-修改密码
	 **/
	@RequestMapping("/v1/findPassword")
	@ResponseBody
	public BaseResult findPassword(@Validated FindPasswordParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return accountController.findPassword(param, request, response);
	}

	/**
	 * 修改密码
	 **/
	@RequestMapping("/v1/updatePassword")
	@ResponseBody
	public BaseResult updatePassword(@Validated UpdatePasswordParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return accountController.updatePassword(param, request, response);
	}

	/**
	 * 账户-获取账号列表
	 **/
	@RequestMapping("/v1/getAccounts")
	@ResponseBody
	public ListResult<RAccountBean> getAccounts(@Validated GetAccountsParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ListResult<RAccountBean> res = new ListResult<RAccountBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return accountController.getAccounts(param, request, response);
	}

	/**
	 * 账户-新建、编辑
	 **/
	@RequestMapping("/v1/edit")
	@ResponseBody
	public BaseResult edit(@Validated EditAccountParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return accountController.edit(param, request, response);
	}

	/**
	 * 账户-重置密码
	 **/
	@RequestMapping("/v1/editPwd")
	@ResponseBody
	public BaseResult editPwd(@Validated EditPwdParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return accountController.editPwd(param, request, response);
	}

	/**
	 * 账户-启用
	 **/
	@RequestMapping("/v1/enable")
	@ResponseBody
	public BaseResult enable(@Validated EnableAccountParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return accountController.enable(param, request, response);
	}

	/**
	 * 账户-禁用
	 **/
	@RequestMapping("/v1/disable")
	@ResponseBody
	public BaseResult disable(@Validated DisableAccountParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return accountController.disable(param, request, response);
	}

	/**
	 * 账户-获取用户角色
	 **/
	@RequestMapping("/v1/getAccountRoles")
	@ResponseBody
	public ListResult<RAccountRolesBean> getAccountRoles(@Validated GetAccountRolesParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ListResult<RAccountRolesBean> res = new ListResult<RAccountRolesBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return accountController.getAccountRoles(param, request, response);
	}

	/**
	 * 账户-获取用户权限
	 **/
	@RequestMapping("/v1/getAccountResources")
	@ResponseBody
	public ListResult<RHaveResourcesBean> getAccountResources(@Validated GetAccountResourcesParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ListResult<RHaveResourcesBean> res = new ListResult<RHaveResourcesBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return accountController.getAccountResources(param, request, response);
	}

	/**
	 * 账户-设置用户下角色
	 **/
	@RequestMapping("/v1/addAcountRoles")
	@ResponseBody
	public BaseResult addAcountRoles(@Validated AddAccountRolesParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return accountController.addAcountRoles(param, request, response);
	}

	/**
	 * 账户-删除用户下角色
	 **/
	@RequestMapping("/v1/delAcountRoles")
	@ResponseBody
	public BaseResult delAcountRoles(@Validated DelAccountRolesParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return accountController.delAcountRoles(param, request, response);
	}

	/**
	 * 账户-删除账号信息
	 **/
	@RequestMapping("/v1/del")
	@ResponseBody
	public BaseResult del(@Validated DelAccountParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return accountController.del(param, request, response);
	}
}
